#define NOBORDER 0
#define SINGLELINE 1
#define DOUBLELINE 2

#define CLOSE 0
#define OPEN  1

#define NORMAL 0
#define ANTI   1

#include "alloc.h"
#include "stdio.h"
#include "conio.h"

typedef struct
{int x,y;
 int width,height;

 int backcolor,textcolor;
 int border;

 int usex,usey;
 int usewidth,useheight;

 int status;
 char *buf;
} VIEW;

void View_init(view, x,y,width,height,backcolor,textcolor,border)
VIEW *view;
int x,y,width,height,backcolor,textcolor,border;
{
 view->x=x;
 view->y=y;
 view->width=width;
 view->height=height;
 view->backcolor=backcolor;
 view->textcolor=textcolor;
 view->border=border;

 if(border==NOBORDER)
    {view->usex=0;
     view->usey=0;
     view->usewidth=width;
     view->useheight=height;
    }
 else
    {
     view->usex=1;
     view->usey=1;
     view->usewidth=width-2;
     view->useheight=height-2;
    }
  view->status=CLOSE;
  view->buf=NULL;
}

int View_open(VIEW *view)
{
  if(view->status==OPEN)
     return 1;

  view->buf=(char *)malloc(2*view->width*view->height);
  if(view->buf==NULL)
     return 0;

  gettext(view->x,view->y,view->x+view->width-1,view->y+view->height-1,view->buf);
  view->status=OPEN;
  textbackground(view->backcolor);
  textcolor(view->textcolor);

  if(view->border!=NOBORDER)
    {int i;
     char l,r,t,b,lt,rt,lb,rb;
     switch(view->border)
	{
	  case SINGLELINE:
		 l=179;r=179;t=196;b=196;
		 lt=218;rt=191;lb=192;rb=217;
		 break;
	  case DOUBLELINE:
		 l=186;r=186;t=205;b=205;
		 lt=201;rt=187;lb=200;rb=188;
		 break;
	 }

      window(1,1,80,25);
      gotoxy(view->x,view->y);
      putch(lt);
      for(i=0;i<view->width-2;i++)
	putch(t);
      putch(rt);

      gotoxy(view->x,view->y+view->height-1);
      putch(lb);
      for(i=0;i<view->width-2;i++)
	putch(b);
	putch(rb);

      for(i=0;i<view->height-2;i++)
	  {
	   gotoxy(view->x,view->y+i+1);
	   putch(l);
	   gotoxy(view->x+view->width-1,view->y+i+1);
	   putch(r);
	  }
	}
 window(view->x+view->usex,view->y+view->usey,view->x+view->usex+view->usewidth-1,view->y+view->usey+view->useheight-1);

 clrscr();
 return 1;
}

void  View_close(VIEW *view)
{if(view->status==CLOSE)
    return;
  puttext(view->x,view->y,view->x+view->width-1,view->y+view->height-1,view->buf);
  free(view->buf);

  view->status=CLOSE;
}

void View_clear(VIEW *view)
{int x0,y0;
 char attr;
 x0=view->x+view->usex;
 y0=view->y+view->usey;
 window(x0,y0,x0+view->usewidth-1,y0+view->useheight-1);
 attr=((view->backcolor<<4)+view->textcolor)&0x7f;
 textattr(attr);
 clrscr();
}

void View_putstring(view, x,y,string,anti)
VIEW *view;
int x,y;
char *string;
int anti;
{
  int x0,y0;
  char attr;
  x0=view->x+view->usex;
  y0=view->y+view->usey;
  window(x0,y0,x0+view->usewidth-1,y0+view->useheight-1);
   gotoxy(x+1,y+1);

   attr=(view->backcolor<<4)+(view->textcolor);

   if(anti==ANTI)
       attr=(~attr)&0x7f;
  textattr(attr);
  cputs(string);
}


























